package com.ruoyi.jjkc.jdjh.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.jjkc.jdjh.domain.Jdjh;
import com.ruoyi.jjkc.jdjh.mapper.JdjhMapper;
import com.ruoyi.jjkc.jdjh.service.IJdjhService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 加计扣除鉴定计划Service业务层处理
 *
 * @author huacai
 * @date 2024-12-07
 */
@Service
public class JdjhServiceImpl implements IJdjhService {
    @Resource
    private JdjhMapper jdjhMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询加计扣除鉴定计划
     *
     * @param jdjhId 加计扣除鉴定计划主键
     * @return 加计扣除鉴定计划
     */
    @Override
    public Jdjh selectJdjhByJdjhId(Integer jdjhId) {
        return jdjhMapper.selectJdjhByJdjhId(jdjhId);
    }

    /**
     * 查询加计扣除鉴定计划列表
     *
     * @param jdjh 加计扣除鉴定计划
     * @return 加计扣除鉴定计划
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Jdjh> selectJdjhList(Jdjh jdjh) {
        return jdjhMapper.selectJdjhList(jdjh);
    }

    /**
     * 查询加计扣除鉴定计划列表
     * sfKJjkc 是否可加计扣除 字段为 是
     * sfzSqLb 是否在申请列表 字段为 否
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Jdjh> selectJdjhListByJjkcSq(Jdjh jdjh) {
        return jdjhMapper.selectJdjhListByJjkcSq(jdjh);
    }

    /**
     * 新增加计扣除鉴定计划
     *
     * @param jdjh 加计扣除鉴定计划
     * @return 结果
     */
    @Override
    public int insertJdjh(Jdjh jdjh) {
        jdjh.setCreateTime(DateUtils.getNowDate());
        return jdjhMapper.insertJdjh(jdjh);
    }

    /**
     * 批量新增加计扣除鉴定计划
     *
     * @param jdjhs 加计扣除鉴定计划List
     * @return 结果
     */
    @Override
    public int batchInsertJdjh(List<Jdjh> jdjhs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(jdjhs)) {
            try {
                for (int i = 0; i < jdjhs.size(); i++) {
                    int row = jdjhMapper.insertJdjh(jdjhs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == jdjhs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改加计扣除鉴定计划
     *
     * @param jdjh 加计扣除鉴定计划
     * @return 结果
     */
    @Override
    public int updateJdjh(Jdjh jdjh) {
        jdjh.setUpdateTime(DateUtils.getNowDate());
        return jdjhMapper.updateJdjh(jdjh);
    }

    /**
     * 批量删除加计扣除鉴定计划
     *
     * @param jdjhIds 需要删除的加计扣除鉴定计划主键
     * @return 结果
     */
    @Override
    public int deleteJdjhByJdjhIds(Integer[] jdjhIds) {
        return jdjhMapper.deleteJdjhByJdjhIds(jdjhIds);
    }

    /**
     * 删除加计扣除鉴定计划信息
     *
     * @param jdjhId 加计扣除鉴定计划主键
     * @return 结果
     */
    @Override
    public int deleteJdjhByJdjhId(Integer jdjhId) {
        return jdjhMapper.deleteJdjhByJdjhId(jdjhId);
    }
}
